package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.stream.Location;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired(required = false)
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCartLast = shoppingCartService.add(shoppingCart);
        return R.success(shoppingCartLast);
    }

    @GetMapping("list")
    public R<List<ShoppingCart>> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        List<ShoppingCart> shoppingCartList  = shoppingCartService.findByUid(userId);
        return R.success(shoppingCartList);
    }

    @DeleteMapping("clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCartService.clean(userId);
        return R.success("清除成功");
    }

    @PostMapping("sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.sub(shoppingCart);
        return R.success("您成功删除一件商品");
    }

}
